<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" width="85%" destroy-on-close
             @closed="$emit('closed')" align-center  :close-on-click-modal="false">
        <el-container>
            <el-aside width="200px" v-loading="showGrouploading">
                <el-container>
                    <el-header>
                        <el-input placeholder="输入关键字进行过滤" v-model="groupFilterText" clearable></el-input>
                    </el-header>
                    <el-main class="nopadding">
                        <el-tree ref="group" class="menu" node-key="id" :data="cateList" :props="cateProps" :current-node-key="''" :highlight-current="true" :expand-on-click-node="false" :filter-node-method="groupFilterNode" @node-click="groupClick"></el-tree>
                    </el-main>
                    <el-footer style="height:51px;">
                        <el-button type="primary" size="small" icon="el-icon-plus" @click="addCate()" v-auth="'applyCate.add'"></el-button>
                        <el-button type="success" size="small" icon="el-icon-edit" @click="editCate()" v-auth="'applyCate.edit'"></el-button>
                        <el-button type="danger" size="small" plain icon="el-icon-delete" @click="delCate" v-auth="'applyCate.delete'"></el-button>
                    </el-footer>
                </el-container>
            </el-aside>
            <el-container>
                <el-header>
                    <div class="left-panel">
                        <el-button type="primary" @click="back">确定</el-button>
                        <el-button type="primary" icon="el-icon-plus" v-auth="'goods.add'" @click="add">新增</el-button>
                    </div>
                    <div class="right-panel">
                        <div class="right-panel-search">
                            <span class="span-title">供应商编号</span>
                            <el-input v-model="search.apply_code" placeholder="供应商编号" clearable></el-input>
                            <span class="span-title">供应商名称</span>
                            <el-input v-model="search.apply_name" placeholder="供应商名称" clearable></el-input>
                            <span class="span-title">联系人</span>
                            <el-input v-model="search.linkman" placeholder="联系人" clearable></el-input>
                            <span class="span-title">联系电话</span>
                            <el-input v-model="search.mobile" placeholder="联系电话" clearable></el-input>
                            <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
                            <el-button type="primary" @click="unsetSearch">重置</el-button>
                        </div>
                    </div>
                </el-header>
                <el-main class="nopadding">
                    <scTable ref="table" row-key="id" :apiObj="apiObj" @selection-change="selectionChange" stripe remoteSort remoteFilter border height="650" :params="query">
                        <el-table-column type="selection" width="50" reserve-selection align="center"></el-table-column>
                        <el-table-column type="index" label="序号" fixed align="center" width="50"></el-table-column>
                        <el-table-column label="分类名称" prop="cate.apply_cate_name" align="center"></el-table-column>
                        <el-table-column label="供应商编号" prop="apply_code" align="center"></el-table-column>
                        <el-table-column label="供应商名称" prop="apply_name" align="center"></el-table-column>
                        <el-table-column label="联系人" prop="linkman" align="center"></el-table-column>
                        <el-table-column label="联系电话" prop="mobile" align="center"></el-table-column>
                        <el-table-column label="联系地址" prop="address" align="center"></el-table-column>
                        <el-table-column label="状态" prop="system_disable" align="center">
                            <template #default="scope">
                                <el-switch v-model="scope.row.system_disable" @change="changeSwitch($event, scope.row)" :loading="scope.row.$switch_status" :active-value="0" :inactive-value="1"></el-switch>
                            </template>
                        </el-table-column>
                        <el-table-column label="简介" prop="desc" align="center"></el-table-column>
                        <!-- <el-table-column label="添加时间" prop="create_time" align="center"></el-table-column> -->
                    </scTable>
                </el-main>
            </el-container>
        </el-container>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
		</template>
        <save-add v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-add>
	</el-dialog>
</template>

<script>
import saveAdd from './save.vue'
export default {
    components: { saveAdd },
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '选择产品',
				edit: '编辑产品',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			showGrouploading: false,
			groupFilterText: '',
			cateList: [],
            cateProps: {
				label: (data) => {
					return data.apply_cate_name
				}
			},
			apiObj: this.$API.apply.apply.lists,
			selection: [],
			search: {
				goods_name: null,
                goods_code:null,
                goods_spec:null,
                goods_model:null,
                material_code:null,
			},
            dialog: {
				save: false,
			},
		}
	},
    watch: {
		groupFilterText(val) {
			this.$refs.group.filter(val);
		}
	},
	mounted() {
        this.getGroup()
	},
	methods: {
		// 显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		// 表单注入数据
		setData(data) {
            this.type = data
		},
        // 选择事件
        selectionChange(selection) {
			this.selection = selection;
		},
        // 选中返回
        back(){
            this.$emit('success', this.selection,this.type)
            this.visible = false;
        },
        //产品新增
		add() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('add')
			})
		},
        // 更新产品新增
        handleSuccess(data, mode) {
			if (mode == 'add') {
				this.$refs.table.refresh()
			} else if (mode == 'edit') {
				this.$refs.table.refresh()
			}
		},
        //搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
        refresh(){
            for (const key in this.search) {
                this.search[key] = null;
            }
            this.$refs.table.upData(this.search)
        },
        //加载树数据
		async getGroup() {
			this.showGrouploading = true;
			var res = await this.$API.apply.applyCate.all.get();
			this.showGrouploading = false;
			var allNode = { id: '', cate_name: '所有' }
			res.data.unshift(allNode);
			this.group = res.data;
		},
		//树过滤
		groupFilterNode(value, data) {
			if (!value) return true;
			return data.cate_name.indexOf(value) !== -1;
		},
		groupClick(node) {
            const ids = this.getAncestorIds(node, this.group);
            this.pid = ids
			var params = {
				cate_id: node.apply_cate_id,
                type: 1
			}
			this.$refs.table.reload(params)
        },
        getAncestorIds(node, tree) {
            const ids = [];
            this.findNodeAndAncestors(node.id, tree, ids);
            return ids;
        },
        findNodeAndAncestors(targetId, tree, ids) {
            for (const item of tree) {
                if (item.id === targetId) {
                ids.unshift(item.id);
                    return true;
                }
                if (item.children && item.children.length > 0) {
                    if (this.findNodeAndAncestors(targetId, item.children, ids)) {
                        ids.unshift(item.id);
                        return true;
                    }
                }
            }
            return false;
        },
	}
}
</script>

<style></style>
